const matrixReshape = (nums, r, c) => {
  const m = nums.length;
  const n = nums[0].length;
  if (r * c !== m * n) {
    return nums;
  }
  const res = new Array(r); // 构建重塑矩阵 res
  for (let i = 0; i < r; i++) {
    res[i] = new Array(c);
  }
  let newR = 0,
    newC = 0;
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      res[newR][newC] = nums[i][j];
      newC++;
      if (newC == c) {
        newR++;
        newC = 0;
      }
    }
  }
  return res;
};
